// 引入 sercice 
const {
    doLogin,
    doLogout,
    createUser,
    editUser,
    getUserList,
    getUser,
    getUserByName,
    remove
} = require("../../service/user/user")

exports.userApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split("/")[1];
    const { id } = ctx.params || "";
    const { name, username, password, creator, pageNum, pageSize, role, role_id, sex, phone, mail, effect, status, avatar, updator } = ctx.request.body || "";
    let res;
    switch (ctx.request.method) {
        case "GET": if (ctx.request.url.length > url.length + 1) {
            res = await getUser({id})
        } else if (url == "searchuser") {
            res = await getUserByName({ name, pageNum, pageSize })
        } else {
            res = await getUserList({ pageNum, pageSize })
        }; break;
        case "POST": if (url == "login") {
            res = await doLogin({ username, password, creator })
        } else if (url == "logout") {
            res = await doLogout({ username, password })
        } else {
            res = await createUser({ username, password, creator, role, role_id })
        }; break;
        case "PUT": res = await editUser({ id, sex, phone, mail, effect, status, avatar, role }); break;
        case "DELETE": res = await remove({ id, updator }); break;
        default: res = { msg: "操作失败", code: "400" }
    }
    ctx.body = res
}